﻿using System.Drawing;
using System.IO;

using Elifart.Library.Serialization;

namespace Elifart.Library
{
    public class TeamManager
    {
        #region Constructor

        private TeamManager()
        {
        }

        #endregion

        #region Singleton

        private static TeamManager _instance;

        public static TeamManager GetManager()
        {
            if (_instance == null)
                _instance = new TeamManager();
            return _instance;
        }

        #endregion

        #region Attributes and Properties

        private TeamCollection _teams;
        public TeamCollection Teams
        {
            get
            {
                if (this._teams == null)
                    this._teams = new TeamCollection();
                return _teams;
            }
        }

        #endregion

        #region Constants

        public const string TEAMS_DIRECTORY = @"..\Elifart\Teams";
        public const string TEAMS_FILE = "Teams.elf";

        public const string TEAMS_BACKUP_DIRECTORY = @"..\Elifart\Backup";
        public const string TEAMS_BACKUP_FILE = "Teams_backup.elf";

        public const string TEAMS_FLAGS_DIRECTORY = @"..\Elifart\Teams\Flags";

        #endregion

        #region Public Methods

        public bool CreateTeamsBackup()
        {
            try
            {
                if (!File.Exists(Path.Combine(TEAMS_DIRECTORY, TEAMS_FILE))) return true;

                if (!Directory.Exists(TEAMS_BACKUP_DIRECTORY))
                    Directory.CreateDirectory(TEAMS_BACKUP_DIRECTORY);

                File.Copy(Path.Combine(TEAMS_DIRECTORY, TEAMS_FILE), Path.Combine(TEAMS_BACKUP_DIRECTORY, TEAMS_BACKUP_FILE), true);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Image GetAssociationFlag(string imageName)
        {
            string flagFile = Path.Combine(TEAMS_FLAGS_DIRECTORY, imageName);
            if (File.Exists(flagFile))
            {
                FileStream stream = new FileStream(flagFile, FileMode.Open);
                Bitmap bitmap = new Bitmap(stream);
                stream.Flush();
                stream.Close();
                return bitmap;
            }
            return null;
        }

        public bool LoadTeams()
        {
            if (!File.Exists(Path.Combine(TEAMS_DIRECTORY, TEAMS_FILE)))
                return true;

            try
            {
                TeamCollection teamCollection =
                    XamlReader.Load<TeamCollection>(Path.Combine(TEAMS_DIRECTORY, TEAMS_FILE));
                this._teams = teamCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool LoadTeamsBackup()
        {
            if (!File.Exists(Path.Combine(TEAMS_BACKUP_DIRECTORY, TEAMS_BACKUP_FILE)))
                return true;

            try
            {
                TeamCollection teamCollection =
                    XamlReader.Load<TeamCollection>(Path.Combine(TEAMS_BACKUP_DIRECTORY, TEAMS_BACKUP_FILE));
                this._teams = teamCollection;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void SaveTeams()
        {
            if (!Directory.Exists(TEAMS_DIRECTORY))
                Directory.CreateDirectory(TEAMS_DIRECTORY);
            if (!Directory.Exists(TEAMS_FLAGS_DIRECTORY))
                Directory.CreateDirectory(TEAMS_FLAGS_DIRECTORY);

            XamlWriter.Save(this.Teams, Path.Combine(TEAMS_DIRECTORY, TEAMS_FILE));
        }

        #endregion
    }
}
